/**
 * Created on Apr 25, 2007
 */
package x.javax.swing.filechooser;

import java.io.FileFilter;
import java.io.IOException;
import java.util.Hashtable;

import javax.swing.Icon;
import javax.swing.UIManager;

import jvs.vfs.FileSystem;
import x.java.io.File;

/**
 * @author qiangli
 * 
 */
public class FileSystemView extends javax.swing.filechooser.FileSystemView {
	private static FileSystem fs = FileSystem.getFileSystem();

	private static Icon rootIcon = null;

	private static Icon folderIcon = null;
	
	private static Icon fileIcon = null;

	private static Hashtable<String, Icon> icons = new Hashtable<String, Icon>();
	/**
	 * 
	 */
	static {
		try {
			//javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView
			//		.getFileSystemView();
			rootIcon = (Icon)UIManager.get("FileView.hardDriveIcon");
			folderIcon = (Icon)UIManager.get("FileView.directoryIcon");
			fileIcon = (Icon)UIManager.get("FileView.fileIcon");
		} catch (Exception e) {

		}
	}

	private static Icon getFileIcon(String name) {
		int idx = name.lastIndexOf(".");
		if (idx == -1) {
			return fileIcon;
		}
		String ext = name.substring(idx);
		Icon icon = (Icon) icons.get(ext);
		if (icon == null) {
			try {
				javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView
						.getFileSystemView();
				java.io.File f = java.io.File.createTempFile("icon", ext);
				icon = fsv.getSystemIcon(f);
				icons.put(ext, icon);
				f.delete();
			} catch (IOException e) {
				icon = fileIcon;
			}
		}
		return icon;
	}

	public FileSystemView() {
	}

	public static javax.swing.filechooser.FileSystemView getFileSystemView() {
		return new FileSystemView();
	}

	public java.io.File createNewFolder(java.io.File containingDir)
			throws IOException {
		java.io.File f = new File(containingDir, "New_Folder");
		if (!f.mkdir()) {
			throw new IOException();
		}
		return f;
	}

	public java.io.File createFileObject(java.io.File dir, String filename) {
		return new File(dir, filename);
	}

	public java.io.File createFileObject(String path) {
		return new File(path);
	}

	protected java.io.File createFileSystemRoot(java.io.File f) {
		return fs.getRoot();
	}

	public java.io.File getChild(java.io.File parent, String fileName) {
		return new File(parent, fileName);
	}

	public java.io.File getDefaultDirectory() {
		return fs.getDefaultDirectory();
	}

	public java.io.File[] getFiles(java.io.File dir, boolean useFileHiding) {
		if (dir == null) {
			return new File[0];
		}

		java.io.File[] files = useFileHiding ? dir.listFiles(new FileFilter() {
			public boolean accept(java.io.File pathname) {
				return pathname.isHidden() == false;
			}

		}) : dir.listFiles();
		if (files == null || files.length == 0) {
			return new File[0];
		}
		return files;
	}

	public java.io.File getHomeDirectory() {
		return fs.getHomeDirectory();
	}

	public java.io.File getParentDirectory(java.io.File dir) {
		if (dir == null) {
			return null;
		}
		return dir.getParentFile();
	}

	public java.io.File[] getRoots() {
		return fs.listRoots();
	}

	public String getSystemDisplayName(java.io.File f) {
		if (isRoot(f)) {
			return "VFS";
		}
		return f.getName();
	}
	
	public static Icon getIcon(java.io.File f) {
		if (fs.isRoot((File)f)) {
			return rootIcon;
		}
		if (f.isDirectory()) {
			return folderIcon;
		}
		return getFileIcon(f.getName());
	}
	
	public Icon getSystemIcon(java.io.File f) {

		if (isRoot(f)) {
			return rootIcon;
		}
		if (f.isDirectory()) {
			return folderIcon;
		}
		return getFileIcon(f.getName());

	}

	public String getSystemTypeDescription(java.io.File f) {
		try {
			if (f.isDirectory()) {
				return "";
			}
			String n = f.getName();
			int idx = n.lastIndexOf(".");
			return n.substring(idx + 1).toLowerCase();
		} catch (Exception e) {
		}
		return null;
	}

	public boolean isComputerNode(java.io.File dir) {
		return false;
	}

	public boolean isDrive(java.io.File dir) {
		return dir.equals(fs.getRoot());
	}

	public boolean isFileSystem(java.io.File f) {
		return true;
	}

	public boolean isFileSystemRoot(java.io.File dir) {
		return fs.isRoot((File) dir);
	}

	public boolean isFloppyDrive(java.io.File dir) {
		return false;
	}

	public boolean isHiddenFile(java.io.File f) {
		return f.isHidden();
	}

	public boolean isParent(java.io.File folder, java.io.File file) {
		return folder.equals(file.getParentFile());
	}

	public boolean isRoot(java.io.File f) {
		return fs.isRoot((File) f);
	}

	public Boolean isTraversable(java.io.File f) {
		return new Boolean(f.isDirectory());
	}
}
